package com.yykj.prescription.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalObjectUtil;
import com.yykj.common.vo.ResultVO;
import com.yykj.prescription.server.dto.prescription.PrescriptionMemberDeleteDTO;
import com.yykj.prescription.server.dto.prescription.PrescriptionMemberEditDTO;
import com.yykj.prescription.server.dto.prescription.PrescriptionSettingResetDTO;
import com.yykj.prescription.server.model.entity.prescription.Prescription;
import com.yykj.prescription.server.model.entity.setting.PrescriptionCheckMember;
import com.yykj.prescription.server.model.entity.setting.PrescriptionSetting;
import com.yykj.prescription.server.model.repository.prescription.PrescriptionRepository;
import com.yykj.prescription.server.model.repository.setting.PrescriptionCheckMemberRepository;
import com.yykj.prescription.server.model.repository.setting.PrescriptionSettingRepository;
import com.yykj.prescription.server.service.PrescriptionSettingService;
import com.yykj.template.server.client.feign.TemplateFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class PrescriptionSettingServiceImpl implements PrescriptionSettingService {

    @Override
    public PrescriptionSetting editPrescriptionSetting(PrescriptionSetting setting) {
        PrescriptionSetting prescriptionSetting;
        if (setting.getHospitalId() == null || setting.getHospitalId() <= 0) {
            throw new NormalException(ResultEnums.PARAM_ERROR, "医院ID必输");
        }
        prescriptionSetting = getPrescriptionSetting(setting.getHospitalId());
        LocalObjectUtil.copyNullValue(setting, prescriptionSetting);
        if (StringUtils.isEmpty(prescriptionSetting.getTitle())) {
            prescriptionSetting.setTitle("医网通互联网医院");
            prescriptionSetting.setSign("https://file.yaoyanglao.com/Resources/20220317/a090db01a8b24a3890915fde9e1c67e4.png");
        } else {
            ResultVO<String> hospitalSign = templateFeignClient.getHospitalSign(prescriptionSetting.getTitle());
            if (hospitalSign.getCode() == 0) {
                prescriptionSetting.setSign(hospitalSign.getData());
            }
        }
        if (Boolean.FALSE.equals(prescriptionSetting.getEndless())) {
            if (prescriptionSetting.getCnt() <= 0) {
                throw new NormalException(ResultEnums.PARAM_ERROR, "最大使用次数不正确");
            }
        }

        if (Boolean.FALSE.equals(prescriptionSetting.getForever())) {
            if (prescriptionSetting.getEffectiveTime() <= 0) {
                throw new NormalException(ResultEnums.PARAM_ERROR, "生效时长不正确");
            }
        }

        prescriptionSetting = settingRepository.save(prescriptionSetting);
        PrescriptionSetting finalPrescriptionSetting = prescriptionSetting;
        new Thread(new Runnable() {
            @Override
            public void run() {
                resetPrescriptionSetting(finalPrescriptionSetting);
            }
        }).start();

        return prescriptionSetting;
    }

    @Override
    public PrescriptionSetting getPrescriptionSetting(Integer hospitalId) {
        PrescriptionSetting setting = settingRepository.findByHospitalId(hospitalId);
        if (setting == null) {
            setting = PrescriptionSetting.defaultSetting(hospitalId);
        }
        return setting;
    }

    @Override
    public void editPrescriptionMember(PrescriptionMemberEditDTO memberEditDTO) {
        List<PrescriptionCheckMember> list = new ArrayList<>();
        for (PrescriptionCheckMember member : memberEditDTO.getMemberList()) {
            PrescriptionCheckMember item = new PrescriptionCheckMember();
            BeanUtils.copyProperties(member, item);
            item.setHospitalId(memberEditDTO.getHospitalId());
            list.add(item);
        }
        memberRepository.saveAll(list);
    }

    @Override
    public void deletePrescriptionMember(PrescriptionMemberDeleteDTO deleteDTO) {
        if (deleteDTO.getId() != null && deleteDTO.getId() >= 0) {
            PrescriptionCheckMember member = memberRepository.findById(deleteDTO.getId()).orElse(null);
            if (member != null) {
                memberRepository.delete(member);
            }
        } else {
            List<PrescriptionCheckMember> memberList = memberRepository.findAllByHospitalIdAndMemberIdAndRole(deleteDTO.getHospitalId(), deleteDTO.getMemberId(), deleteDTO.getRole());
            if (memberList != null && memberList.size() > 0) {
                memberRepository.deleteAll(memberList);
            }
        }

    }

    @Override
    public List<PrescriptionCheckMember> listPrescriptionCheckMember(Integer hospitalId) {
        return memberRepository.findByHospitalId(hospitalId);
    }

    @Override
    public void resetPrescriptionSetting(PrescriptionSetting prescriptionSetting) {
        //处方当前状态：1未审核，2已审核，3已失效，4作废,5已流转
        List<Prescription> prescriptionList = prescriptionRepository.findAllByHospitalIdAndStatusIn(prescriptionSetting.getHospitalId(), Arrays.asList(1, 2, 5));
        PrescriptionSettingResetDTO settingDTO = new PrescriptionSettingResetDTO();
        BeanUtils.copyProperties(prescriptionSetting, settingDTO);
        for (Prescription prescription : prescriptionList) {
            BeanUtils.copyProperties(settingDTO, prescription);
        }
        prescriptionRepository.saveAll(prescriptionList);
    }


    private PrescriptionSettingRepository settingRepository;

    private PrescriptionCheckMemberRepository memberRepository;

    private PrescriptionRepository prescriptionRepository;

    @Resource
    private TemplateFeignClient templateFeignClient;

    @Autowired
    public void setSettingRepository(PrescriptionSettingRepository settingRepository) {
        this.settingRepository = settingRepository;
    }

    @Autowired
    public void setMemberRepository(PrescriptionCheckMemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    @Autowired
    public void setPrescriptionRepository(PrescriptionRepository prescriptionRepository) {
        this.prescriptionRepository = prescriptionRepository;
    }
}
